/* 获取验证码 */
function getCaptcha() {
    let email = document.getElementById("email").value
    let re = /^\w+@[a-z0-9]+\.[a-z]{2,}$/
    if (!re.test(email)) {
        alert("邮箱格式错误")
        return
    }

    let xhr = new XMLHttpRequest()
    xhr.open('GET', '/sendCaptcha?email=' + email)
    xhr.onload = function (e) {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                // 发送成功，不应该使用 alert！！！
                alert("发送验证码成功")
            } else {
                alert("发送验证码失败")
            }
        }
    }
    xhr.onerror = function (e) {
        console.error(xhr.statusText)
    }
    xhr.send(null)
}


/* 两次密码比对 */
function checkPassword() {
    let password = document.getElementById("password").value
    let passwordConfirm = document.getElementById("password-confirm").value
    if (password !== passwordConfirm) {
        console.log('两次密码不一致');
        document.querySelector("#password-confirm+.msgTip").innerText = "两次密码不一致"
        document.getElementById("registerBtn").style.visibility = "hidden"
    } else {
        document.querySelector("#password-confirm+.msgTip").innerText = ""
        document.getElementById("registerBtn").style.visibility = "visible"
    }
}


document.getElementById('getCaptcha').onclick = getCaptcha
document.getElementById('password').onblur = checkPassword
document.getElementById('password-confirm').onblur = checkPassword
